package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author LQQ
 * @Date 2020/3/11 16:08
 * @Version 1.0
 */

@RequestMapping("/cart")
@RestController
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private TokenDecode tokenDecode;//令牌解析

    @GetMapping("/addCart")
    public Result addCart(@RequestParam("skuId") String skuId, @RequestParam("num") Integer num) {

        //动态获取当前登入人信息
     //   String username = "itcast";
        String username = tokenDecode.getUserInfo().get("username");

        cartService.addCart(skuId, num, username);

        return new Result(true, StatusCode.OK, "添加购物车成功");
    }


    /**
     * 查询购物车列表数据
     *
     * @return
     */
    @GetMapping("/list")
    public Map list() {
        //动态获取当前登入人信息
    //    String username = "itcast";

        String username = tokenDecode.getUserInfo().get("username");


        Map map = cartService.list(username);

        return map;
    }


}
